package com.lepu.app.fun.grow.play;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.core.lib.application.BaseFragment;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;

public class PlayUserFragment extends BaseFragment {

	private View mMainView;
	private boolean mInitDataSuccess = false;

	private TextView mUserTitleTextView;
	private ImageView mUserImageView;
	private TextView mUserBottomTextView;
	private RelativeLayout mBottomMenuLayout;

	@SuppressLint("InflateParams")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mMainView = LayoutInflater.from(getActivity()).inflate(
				R.layout.grow_play_user_fragment, null, false);
		init();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
		}
		return mMainView;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();

		mInitDataSuccess = false;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);

		if (isVisibleToUser) {
			if (!mInitDataSuccess) {
				initData();
			}
		}
	}

	private void init() {
		mUserTitleTextView = (TextView) mMainView
				.findViewById(R.id.userTitleTextView);
		mUserImageView = (ImageView) mMainView.findViewById(R.id.userImageView);
		mUserBottomTextView = (TextView) mMainView
				.findViewById(R.id.userBottomTextView);

		mBottomMenuLayout = (RelativeLayout) mMainView
				.findViewById(R.id.bottomMenuLayout);
		ImageView menuAutoImageView = (ImageView) mMainView
				.findViewById(R.id.menuAutoImageView);
		ImageView menuCancelImageView = (ImageView) mMainView
				.findViewById(R.id.menuCancelImageView);
		menuAutoImageView.setOnClickListener(mOnClickListener);
		menuCancelImageView.setOnClickListener(mOnClickListener);

		mUserImageView.setOnClickListener(mOnClickListener);
	}

	private void initData() {
		Bundle data = getArguments();
		String title = data.getString("title");
		String imagePath = data.getString("imagePath");
		int index = data.getInt("index");
		String bottom = data.getString("bottom");

		mUserTitleTextView.setText(title);
		mUserBottomTextView.setText(bottom);

		if (PlayImageUserActivity.getInstance().mIsPlayNet) {
			
//			Bitmap bitmap = ImageUtilBase.getBitmap(imagePath);
//			mUserImageView.setImageBitmap(bitmap);
			
			UniversalImageLoadTool.disPlay("file://"+imagePath, mUserImageView, 0);
			
		} else if (PlayImageUserActivity.getInstance().mIsSample) {
			
			String path = String.format("image_sample/%d.jpg", index);
//			try {
//				AssetManager assetManager = getActivity().getAssets();
//				InputStream inputStream = assetManager.open(path);
//				Bitmap bitmap = ImageUtilBase.getBitmap(inputStream);
//				mUserImageView.setImageBitmap(bitmap);
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
			UniversalImageLoadTool.disPlay(ImageUtilBase.getAssetImageUrl(path), mUserImageView,0);
			
		} else {
//			Bitmap bitmap = ImageUtilBase.getBitmap(imagePath);
//			mUserImageView.setImageBitmap(bitmap);
			
			UniversalImageLoadTool.disPlay("file://"+imagePath, mUserImageView, 0);
		}

		mInitDataSuccess = true;
	}

	@SuppressWarnings("unused")
	private void showSelectPlayPop() {
		String[] array = getResources().getStringArray(
				R.array.array_pop_play_auto);
		new MyDialog(getActivity())
				.setSimpleItems(array, new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (which == 0) {
							((BaseFragmentActivity) getActivity()).finish(true);

							Intent intent = new Intent(getActivity(),
									PlayImageAutoActivity.class);
							((BaseFragmentActivity) getActivity())
									.startActivity(intent, true);
						} else {
							((BaseFragmentActivity) getActivity()).finish(true);
						}
					}
				}).create(null).show();
	}

	private View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.userImageView:

				// 弹出选择menu
				// showSelectPlayPop();

				int visible = mBottomMenuLayout.getVisibility();
				if (visible == View.GONE) {
					mBottomMenuLayout.setVisibility(View.VISIBLE);
					mUserBottomTextView.setVisibility(View.GONE);
				} else {
					mBottomMenuLayout.setVisibility(View.GONE);
					mUserBottomTextView.setVisibility(View.VISIBLE);
				}

				break;
			case R.id.menuAutoImageView:

				PlayImageUserActivity userActivity = ((PlayImageUserActivity) getActivity());

				Intent intent = new Intent(getActivity(),
						PlayImageAutoActivity.class);
				intent.putExtra("boolean_sample", userActivity.mIsSample);
				intent.putExtra("boolean_all", userActivity.mIsPlayAll);// 播放当天的
				intent.putExtra("current_day", userActivity.mCurrentDateString);
				intent.putExtra("IsPlayNet", userActivity.mIsPlayNet);
				intent.putExtra("AlbumId", userActivity.mAlbumID);
				intent.putExtra("titleArray", userActivity.mNetTitleArray);
				((BaseFragmentActivity) getActivity()).startActivity(intent,
						true);

				userActivity.finish(true);
				break;
			case R.id.menuCancelImageView:

				((BaseFragmentActivity) getActivity()).finish(true);
				break;

			default:
				break;
			}
		}
	};

}
